package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;
import java.util.Set;

import mulMan.domain.AudioFile;
import mulMan.domain.Playlist;
import mulMan.gui.view.AudioFilesView;
import mulMan.gui.view.ModifyAudioFileView;
import mulMan.gui.view.PlaylistsPopupView;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AudioFilesController extends Observable implements ApplicationContextAware{
	
	private ApplicationContext applicationContext;
	private Set<AudioFile> audioFiles;
	private AudioFilesView view;
	
	public Set<AudioFile> getAudiosFile() {
		return audioFiles;
	}
	public void setAudioFiles(Set<AudioFile> audioFiles) {
		this.audioFiles = audioFiles;
	}

	public AudioFilesView getView() {
		return view;
	}
	public void setView(AudioFilesView view) {
		this.view = view;
	}
	
	
	public AudioFilesController(Set<AudioFile> audioFiles, AudioFilesView view) {
		this.audioFiles = audioFiles;
		this.view = view;
		
		view.addTableAudioFilesListener(new TableAudioFilesListener());
		view.addDeleteAudioFileListener(new DeleteAudioFileListener());
		
	}
	
	public AudioFile getSelectedAudioFile() {
		return (AudioFile) audioFiles.toArray()[view.getTableOfAudioFiles().getSelectedRow()];
	}
	
	
	//ACTION LISTENERS
	class TableAudioFilesListener implements MouseListener {
		
		private boolean playlistsListView = false;

		public void mouseClicked(MouseEvent e) {
			if(e.getClickCount() == 2){
				
	            new AudioFileController((AudioFile) audioFiles.toArray()[view.getTableOfAudioFiles().getSelectedRow()], 
	            new ModifyAudioFileView((AudioFile) audioFiles.toArray()[view.getTableOfAudioFiles().getSelectedRow()]));
			}
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}

		@SuppressWarnings("unchecked")
		private void showPopup(MouseEvent e) {
			if (playlistsListView == false) {
				PlaylistsPopupView playlistsPopupView = new PlaylistsPopupView((List<Playlist>) applicationContext.getBean("listOfPlaylists"));
				PlaylistsController playlistsController = (PlaylistsController) applicationContext.getBean("playlistsController", (List<Playlist>) applicationContext.getBean("listOfPlaylists"), playlistsPopupView);
				
				setAudioFilesControllerToPlaylistsController(playlistsController);
				
				view.getPopupMenu().add(playlistsPopupView);
				
				playlistsListView = true;
			}
			
			view.getTableOfAudioFiles().getSelectionModel().setSelectionInterval(view.getTableOfAudioFiles().rowAtPoint(e.getPoint()), view.getTableOfAudioFiles().rowAtPoint(e.getPoint()));
			view.getPopupMenu().show(view.getTableOfAudioFiles(), e.getX(), e.getY());
		}
    }
	
	
	class DeleteAudioFileListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			try {
				audioFiles.remove(view.getTableOfAudioFiles().getSelectedRow());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}


	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		applicationContext = arg0;
	}
	public void setAudioFilesControllerToPlaylistsController(PlaylistsController playlistsController) {
		playlistsController.setAudioFilesController(this);
	}
}
